import json

from django.views.decorators.csrf import csrf_exempt

from utils.response_utils import *
from utils.session_utils import *

from star_sys.models import StarPaper, StarScholar

from elastic_sys.elastic.elastic_manager import get_entity, get_entity_list, entity_exists
from elastic_sys.elastic.entity_definer import get_entity_enum_by_type_name, EntityEnum


@csrf_exempt
def paper_add(request):
    if request.method != 'POST':
        return method_disallowed_response(request.method, 'POST')

    if not has_login(request):
        return login_status_response()

    user = get_login_user(request)
    data = json.loads(request.body)
    paper_id = data.get('id')

    if not entity_exists(EntityEnum.paper, paper_id):
        return build_response(errno=2001, msg=f"实体{EntityEnum.paper} {paper_id}不存在")

    if StarPaper.objects.filter(user_id=user, paper_id=str(paper_id)).exists():
        return build_response(errno=2002, msg="当前已收藏")

    StarPaper.objects.create(user_id=user, paper_id=str(paper_id))
    return build_response(msg="收藏成功")


@csrf_exempt
def paper_remove(request):
    if request.method != 'POST':
        return method_disallowed_response(request.method, 'POST')

    if not has_login(request):
        return login_status_response()

    user_id = get_login_id(request)
    data = json.loads(request.body)
    paper_id = data.get('id')

    if not entity_exists(EntityEnum.paper, paper_id):
        return build_response(errno=2001, msg=f"实体{EntityEnum.paper} {paper_id}不存在")

    star_paper_to_delete = StarPaper.objects.filter(user_id=user_id, paper_id=str(paper_id)).first()

    if not star_paper_to_delete:
        return build_response(errno=2002, msg="当前未收藏")

    star_paper_to_delete.delete()
    return build_response(msg="取消收藏成功")


@csrf_exempt
def paper_ask(request):
    if request.method != 'GET':
        return method_disallowed_response(request.method, 'GET')

    if not has_login(request):
        return login_status_response()

    user_id = get_login_id(request)
    paper_id = request.GET.get('id', None)
    result = StarPaper.objects.filter(user_id=user_id, paper_id=str(paper_id)).exists()
    return build_response(other_info={"result": result}, msg="获取成功")


@csrf_exempt
def paper_list(request):
    if request.method != 'GET':
        return method_disallowed_response(request.method, 'GET')

    if not has_login(request):
        return login_status_response()

    user_id = get_login_id(request)
    star_papers = list(StarPaper.objects.filter(user_id=user_id))
    paper_id_list = [star_paper.paper_id for star_paper in star_papers]
    papers_dict = get_entity_list(EntityEnum.paper, paper_id_list, dict_type=True) if paper_id_list else []
    papers_list = [({**papers_dict[star_paper.paper_id], 'star_time': star_paper.star_time.strftime('%Y-%m-%d %H:%M:%S')}
                    if papers_dict[star_paper.paper_id] is not None else {'id': star_paper.paper_id, 'star_time': star_paper.star_time.strftime('%Y-%m-%d %H:%M:%S')})
                   for star_paper in star_papers]
    return build_response(other_info={"result": papers_list}, msg="获取成功")


@csrf_exempt
def scholar_add(request):
    if request.method != 'POST':
        return method_disallowed_response(request.method, 'POST')

    if not has_login(request):
        return login_status_response()

    user = get_login_user(request)
    data = json.loads(request.body)
    scholar_id = data.get('id')

    if not entity_exists(EntityEnum.scholar, scholar_id):
        return build_response(errno=2001, msg=f"实体{EntityEnum.scholar} {scholar_id}不存在")

    if StarScholar.objects.filter(user_id=user, scholar_id=str(scholar_id)).exists():
        return build_response(errno=2002, msg="当前已收藏")

    StarScholar.objects.create(user_id=user, scholar_id=str(scholar_id))
    return build_response(msg="收藏成功")


@csrf_exempt
def scholar_remove(request):
    if request.method != 'POST':
        return method_disallowed_response(request.method, 'POST')

    if not has_login(request):
        return login_status_response()

    user_id = get_login_id(request)
    data = json.loads(request.body)
    scholar_id = data.get('id')

    if not entity_exists(EntityEnum.scholar, scholar_id):
        return build_response(errno=2001, msg=f"实体{EntityEnum.scholar} {scholar_id}不存在")

    star_scholar_to_delete = StarScholar.objects.filter(user_id=user_id, scholar_id=str(scholar_id)).first()

    if not star_scholar_to_delete:
        return build_response(errno=2002, msg="当前未收藏")

    star_scholar_to_delete.delete()
    return build_response(msg="取消收藏成功")


@csrf_exempt
def scholar_ask(request):
    if request.method != 'GET':
        return method_disallowed_response(request.method, 'GET')

    if not has_login(request):
        return login_status_response()

    user_id = get_login_id(request)
    scholar_id = request.GET.get('id', None)
    result = StarScholar.objects.filter(user_id=user_id, scholar_id=str(scholar_id)).exists()
    return build_response(other_info={"result": result}, msg="获取成功")


@csrf_exempt
def scholar_list(request):
    if request.method != 'GET':
        return method_disallowed_response(request.method, 'GET')

    if not has_login(request):
        return login_status_response()

    user_id = get_login_id(request)
    star_scholars = list(StarScholar.objects.filter(user_id=user_id))
    scholar_id_list = [star_scholar.scholar_id for star_scholar in star_scholars]
    scholars_dict = get_entity_list(EntityEnum.scholar, scholar_id_list, dict_type=True) if scholar_id_list else []
    scholars_list = [
        {**scholars_dict[star_scholar.scholar_id], 'star_time': star_scholar.star_time.strftime('%Y-%m-%d %H:%M:%S')}
        if scholars_dict[star_scholar.scholar_id] is not None else {'id': star_scholar.scholar_id, 'star_time': star_scholar.star_time.strftime('%Y-%m-%d %H:%M:%S')}
        for star_scholar in star_scholars]
    return build_response(other_info={"result": scholars_list}, msg="获取成功")
